package com.hmdp.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 邮件发送工具类
 */
@Slf4j
@Service
public class SendMailUtil {

    @Resource
    private JavaMailSender javaMailSender;

    //发送人
    String from = "yhans0923@qq.com";

    //正文

    String text = "\n\n%s,您好!\n        欢迎使用黑马点评，您的验证码为%s。该验证码仅在5分钟内有效，请勿泄露于他人！祝您生活愉快";


    @Async
    public void sendCode(String phone, String code) {
        sendMail2(phone, code);
        // 5.发送验证码
        log.debug("发送短信验证码成功，验证码：{}", code);
    }

    //邮箱验证码
    public void sendMail1(String sendToEmail, String code) {

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from + "(【黑马点评】)");
        //拼接邮箱后缀
        sendToEmail += "@qq.com";
        message.setTo(sendToEmail);

        //标题
        String subject = "尊敬的黑马点评用户" + sendToEmail.substring(0, 10);
        message.setSubject(subject);

        //邮件内容
        String context = String.format(text, code);
        message.setText(context);

        javaMailSender.send(message);
    }

    //手机验证码
    public void sendMail2(String phone, String code) {

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from + "(【黑马点评】)");
        message.setTo("yhans0923@qq.com");

        //标题
        String subject = "尊敬的用户" + phone;
        message.setSubject(subject);

        //邮件内容
        String context = String.format(text, phone, code);

        message.setText(context);

        javaMailSender.send(message);
    }
}
